<template>
  <div class="div">
    <a-row class="top_font">
      <a-col span="5" class="">
        <a-input addon-before="标题：" v-model="search.title"/>
      </a-col>
      <a-col span="5">
        <a-input style="margin-left: 16px" addon-before="内容：" v-model="search.comment"/>
      </a-col>
      <a-col span="1">
        <a-button
          style="margin-left: 64px"
          icon="search"
          type="primary"
          htmlType="submit"
          @click="searchList()"
        >查询</a-button>
      </a-col>
    </a-row>
    <a-row class="center_margin">
      <a-col span="5">
        <a-input addon-before="修改人：" v-model="search.updateName"/>
      </a-col>
      <a-col span="5">
        <a-input style="margin-left: 16px" addon-before="修改时间：" v-model="search.updateTime"/>
      </a-col>
      <a-col span="1">
        <a-button
          icon="sync"
          style="margin-left: 64px"
          @click="reset()"
        >重置</a-button>
      </a-col>
    </a-row>
    <a-row style="margin-top:10px">
      <a-button
        icon="plus"
        type="primary"
        htmlType="submit"
        @click="add()"
      >新增</a-button>
    </a-row>
    <a-row class="bottom_font">
      <a-col class="title" span="4">
        编号
      </a-col>
      <a-col class="title" span="4">
        标题
      </a-col>
      <a-col class="title" span="4">
        内容
      </a-col>
      <a-col class="title" span="4">
        修改人
      </a-col>
      <a-col class="title" span="4">
        修改日期
      </a-col>
      <a-col class="title" span="4">
        操作
      </a-col>
    </a-row>
    <a-list :data-source="data" :pagination="{onChange: (current, size) => {this.num = current,this.searchViews?this.searchInfo():this.getAll()}, showQuickJumper: true, pageSize: 15, total: totalList}">
      <a-list-item slot="renderItem" slot-scope="item,index">
        <a-col class="numb" span="4">
          {{ item.id }}
        </a-col>
        <a-col class="numb" span="4">
          {{ item.title }}
        </a-col>
        <a-col class="numb" span="4">
          {{ item.comment }}
        </a-col>
        <a-col class="numb" span="4">
          {{ item.updateName }}
        </a-col>
        <a-col class="numb" span="4">
          {{ item.updateTime }}
        </a-col>
        <a-col class="numb" span="4">
          <a-button @click="edit(index)" type="link" size="small" class="operate_change">编辑</a-button>
          <a-button @click="del(index)" type="link" size="small" class="operate_change">删除</a-button>
        </a-col>
      </a-list-item>
    </a-list>
    <a-modal
      title="信息修改"
      style="top: 20px;"
      :width="800"
      v-model="editAnnouncementInfo"
      @ok="handleOk()"
    >
      <a-form class="permission-form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标题" hasFeedback>
          <a-input v-model="title"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内容" hasFeedback>
          <a-input v-model="comment"/>
        </a-form-item>
      </a-form>
    </a-modal>
    <a-modal
      title="新增公告"
      style="top: 20px;"
      :width="800"
      v-model="addAnnouncementInfo"
      @ok="addHandleOk()"
    >
      <a-form class="permission-form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标题" hasFeedback>
          <a-input v-model="addInfo.title"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内容" hasFeedback>
          <a-input v-model="addInfo.comment"/>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { addAnnouncement, delAnnouncement, searchAnnouncement, updateAnnouncement, getAllAnnouncement } from '@/api/manage'
export default {
  data () {
    return {
      id: '',
      data: [],
      num: 1,
      totalList: 0,
      addAnnouncementInfo: false,
      searchViews: false,
      addInfo: { title: '', comment: '' },
      search: { pageSize: 15, pageNum: 1, updateTime: '', updateName: '', title: '', comment: '' },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      comment: '',
      title: '',
      editAnnouncementInfo: false
    }
  },
  created () {
    this.getAll()
  },
  methods: {
    searchList () {
      this.searchViews = true
      this.searchInfo()
      this.num = 1
    },
    searchInfo () {
      this.$set(this.search, 'pageNum', this.num)
      searchAnnouncement(this.search).then((res) => {
        this.data = res.datalist
        this.totalList = Number(res.total)
        // console.log(res)
        // console.log(this.data)
      })
    },
    reset () {
      this.$set(this.search, 'pageNum', 1)
      this.$set(this.search, 'updateName', '')
      this.$set(this.search, 'updateTime', '')
      this.$set(this.search, 'title', '')
      this.$set(this.search, 'comment', '')
      this.num = 1
      if (this.searchViews) {
        this.getAll()
      }
      this.searchViews = false
    },
    getAll () {
      var that = this
      const pageBase = { content: '', pageNum: this.num, pageSize: 15 }
      getAllAnnouncement(pageBase).then((res) => {
        // console.log(res)
        that.data = res.datalist
        that.totalList = Number(res.total)
      })
    },
    del (index) {
      // console.log('删除')
      const Id = { 'id': this.data[index].id }
      delAnnouncement(Id).then((res) => {
        // console.log('success')
      })
    },
    edit (index) {
      // console.log('编辑')
      this.editAnnouncementInfo = true
      this.title = this.data[index].title
      this.comment = this.data[index].comment
      this.id = this.data[index].id
    },
    handleOk () {
      this.editAnnouncementInfo = false
      const update = { title: this.title, comment: this.comment, id: this.id }
      updateAnnouncement(update).then((res) => {
        // console.log('success')
      })
    },
    add () {
      this.addAnnouncementInfo = true
    },
    addHandleOk () {
      addAnnouncement(this.addInfo).then((res) => {
        this.addAnnouncementInfo = false
        // console.log('success')
      })
    }
  }
}
</script>

<style scoped>
.title{
  text-align: center;
}
.div{
    padding-right: 20px;
    padding-left: 20px;
    padding-top: 20px;
    padding-bottom: 40px;
    background-color: rgb(255, 255, 255);
    width: 100%;
    height: 100%;
}
.inputround{
    border-radius: 25px;
    border-width: 1pX;
    width: 50%;
}
.top_font{
    text-align: right;
    font-size: small;
}
.center_margin{
    text-align: right;
    margin-top: 10px;
    font-size: small;
}
.img{
    height: 20px;
    width: 20px;
    border-radius: 100%;
    margin-left: 64px
}
.bottom_font{
    text-align: center;
    font-size: larger;
    margin-top: 10px;
    background-color: rgba(241, 247, 247, 0.979);

}
.numb{
  text-align: center;
  word-break:break-all;
	display:-webkit-box;
	-webkit-line-clamp:1;
	-webkit-box-orient:vertical;
	overflow:hidden;
}
.operate_change{
    color: chartreuse;
}
</style>
